<?php
class BbsAccessException extends WException {}		//	잘못된접근
class BbsPermissionException extends WException {}	//	접근 권한이 없음.
class Cl_Bbs extends WdkClass 
{
	protected
		/******************************************************/
		//	원본 테이블 정보
		/******************************************************/
		$bbs_seq				= 0			,	//
		$bbs_id					= ''		,	//	
		$bbs_name				= ''		,	//	
		$bbs_skin				= 'default'	,	//		
		$bbs_listsize			= 15		,	//	
		$bbs_pagesize			= 10		,	//	
		$bbs_header				= ''		,	//	
		$bbs_footer				= ''		,	//
		//	관리자 설정	
		$bbs_admin				= ''		,	//	
		//	추가 옵션 설정 필드들..
		$bbs_comment_flag		= 'T'		,	//	
		$bbs_recommend_flag		= 'T'		,	//		
		$bbs_secure_flag		= 'F'		,	//	
		$bbs_post_flag			= 'T'		,	//	
		$bbs_tag_flag			= 'T'		,	//	
		$bbs_linked_flag		= 'F'		,	//	
		//	업로드 설정 필드들..	
		$bbs_file_flag			= 'F'		,	//	
		$bbs_file_num			= 1			,	//	
		$bbs_file_size			= 2			,	//	
		$bbs_file_type			= '*'		,	//	
		//	권한/포인트 설정 		
		$bbs_list_level			= 0			,	//	
		$bbs_view_level			= 0			,	//	
		$bbs_write_level		= 0			,	//
		$bbs_write_point		= 0			,	//	
		$bbs_view_point			= 0			,	//	
		$bbs_comment_point		= 0			,	//
		//	카테고리 설정
		$bbs_categories			= array()	,	//
		$bbs_time				= 0			,	//	
		$bbs_del_flag			= 'F'		,	//	
		/******************************************************/
		//	추가 정보
		/******************************************************/
		$bbs_admin_arr			= array()	,	//
		$bbs_categories_arr		= array()	;	//
	
	
	/**
	 * 	
	 */
	function __construct()
	{
		parent::__construct();
		self::getDbo();
	}
	
	/**
	 *	
	 */
	public function __init($data)
	{
		parent::__init($data);
		self::dformat();
		$this->session = Wdk::helper('Session');
	}
	
	/**
	 *	
	 */
	public function load($bbs_seq)
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		/*------------------------------------------------------------*/
		self::__init($dbo->cLoad($bbs_seq));
	}
	
	/**
	 *	
	 */
	public function loadById($bbs_id)
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		/*------------------------------------------------------------*/
		self::__init($dbo->cLoadById($bbs_id));
	}
	
	/**
	 *	
	 */
	public function isAdmin()
	{
		return ($this->session->isAdmin() || in_array($this->session['user_seq'], $this->bbs_admin_arr));
	}
	public function isListable()
	{
		if($this->isAdmin()
		||(int) $this->session['user_group_level'] >= $this->bbs_list_level)
		{
			return true;
		}
		return false;
	}
	public function isViewable()
	{
		if($this->isAdmin()
		||(int) $this->session['user_group_level'] >= $this->bbs_view_level)
		{
			return true;
		}
		return false;
	}
	public function isWritable()
	{
		if($this->isAdmin()
		||(int) $this->session['user_group_level'] >= $this->bbs_write_level)
		{
			return true;
		}
		return false;
	}
	
	/**
	 *	
	 */
	public function dformat()
	{
		$this->bbs_admin_arr		= explode(",", $this->bbs_admin);
		$this->bbs_categories_arr	= explode(",", $this->bbs_categories);
	}
	
	/**
	 * 
	 */
	public function autoUpdate($log='')
	{
		if((int)$this->bbs_seq > 0)
		{
			$this->update($log);
		}
		else
		{
			$this->insert($log);
		}
	}
	
	/**
	 *	
	 */
	public function insert($log='Insert')
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		/*------------------------------------------------------------*/
		
		$this->bbs_time	= time();
		$this->bbs_del_flag		= 'F';
		
		$data = array(	'bbs_id'				=>	$this->bbs_id,
						'bbs_name'				=>	$this->bbs_name,
						'bbs_skin'				=>	$this->bbs_skin,
						'bbs_listsize'			=>	$this->bbs_listsize,
						'bbs_pagesize'			=>	$this->bbs_pagesize,
						'bbs_header'			=>	$this->bbs_header,
						'bbs_footer'			=>	$this->bbs_footer,
						'bbs_admin'				=>	$this->bbs_admin,
						'bbs_comment_flag'		=>	$this->bbs_comment_flag,
						'bbs_recommend_flag'	=>	$this->bbs_recommend_flag,
						'bbs_secure_flag'		=>	$this->bbs_secure_flag,
						'bbs_linked_flag'		=>	$this->bbs_linked_flag,
						'bbs_tag_flag'			=>	$this->bbs_tag_flag,
						'bbs_file_flag'			=>	$this->bbs_file_flag,
						'bbs_file_num'			=>	$this->bbs_file_num,
						'bbs_file_size'			=>	$this->bbs_file_size,
						'bbs_file_type'			=>	$this->bbs_file_type,
						'bbs_list_level'		=>	$this->bbs_list_level,
						'bbs_view_level'		=>	$this->bbs_view_level,
						'bbs_write_level'		=>	$this->bbs_write_level,
						'bbs_write_point'		=>	$this->bbs_write_point,
						'bbs_view_point'		=>	$this->bbs_view_point,
						'bbs_comment_point'		=>	$this->bbs_comment_point,
						'bbs_categories'		=>	$this->bbs_categories,
						'bbs_time'				=>	$this->bbs_time
					);
		$this->bbs_seq = $dbo->insert($data);
		/******************************************************/
		parent::setBackupData(array_keys($data));
		$this->backup($log);
		/******************************************************/
	}
	
	/**
	 * 
	 */
	public function update($log='update')
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		/*------------------------------------------------------------*/
		
		if(parent::isUpdatable())
		{
			$data = array(	'bbs_name'				=>	$this->bbs_name,
							'bbs_skin'				=>	$this->bbs_skin,
							'bbs_listsize'			=>	$this->bbs_listsize,
							'bbs_pagesize'			=>	$this->bbs_pagesize,
							'bbs_header'			=>	$this->bbs_header,
							'bbs_footer'			=>	$this->bbs_footer,
							'bbs_admin'				=>	$this->bbs_admin,
							'bbs_comment_flag'		=>	$this->bbs_comment_flag,
							'bbs_recommend_flag'	=>	$this->bbs_recommend_flag,
							'bbs_secure_flag'		=>	$this->bbs_secure_flag,
							'bbs_linked_flag'		=>	$this->bbs_linked_flag,
							'bbs_tag_flag'			=>	$this->bbs_tag_flag,
							'bbs_file_flag'			=>	$this->bbs_file_flag,
							'bbs_file_num'			=>	$this->bbs_file_num,
							'bbs_file_size'			=>	$this->bbs_file_size,
							'bbs_file_type'			=>	$this->bbs_file_type,
							'bbs_list_level'		=>	$this->bbs_list_level,
							'bbs_view_level'		=>	$this->bbs_view_level,
							'bbs_write_level'		=>	$this->bbs_write_level,
							'bbs_write_point'		=>	$this->bbs_write_point,
							'bbs_view_point'		=>	$this->bbs_view_point,
							'bbs_comment_point'		=>	$this->bbs_comment_point,
							'bbs_categories'		=>	$this->bbs_categories
					);
			$dbo->update($data, array("bbs_seq = ".$this->bbs_seq));
			/******************************************************/
			parent::setBackupData(array_keys($data));
			$this->backup($log);
			/******************************************************/
		}
	}
	
	/**
	 * 
	 */
	public function delete($log='delete')
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		/*------------------------------------------------------------*/
		$dbo->delete(array("bbs_seq = ".$this->bbs_seq));
		/******************************************************/
		parent::setBackupData();
		$this->backup($log);
		/******************************************************/
	}
	
	/**
	 *	
	 */
	private function backup($log='')
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		/*------------------------------------------------------------*/
		$dbo->log($this->bbs_seq, $log, $this->backup_data);
	}
	
	/**
	 * 
	 */
	public function getDbo()
	{
		return Wdk::model(substr(__CLASS__, 3));
	}
}